/**
 * 后台用户登录验证以及操作文章的所有接口
 * @author 杨执信
 * @controller {MainController}
*/
/* eslint-disable*/
'use strict'

const Controller = require('egg').Controller

class MainController extends Controller {

  //判断用户名密码是否正确
  async checkLogin() {
    let username = this.ctx.request.body.username
    let password = this.ctx.request.body.password
    console.log(username, password, '+++++++++')
    const sql = " SELECT username FROM admin_user WHERE username = '" + username +
      "' AND password = '" + password + "'"

    const res = await this.app.mysql.query(sql)
    if (res.length > 0) {
      //登录成功,进行session缓存
      let openId = new Date().getTime()
      this.ctx.session.openId = { 'openId': openId }
      // 向前端返回登陆成功的信息
      this.ctx.body = {
        data: null,
        openId: openId,
        code: 200,
        msg: '登陆成功'
      }
    } else {
      this.ctx.body = {
        data: '登录失败',
        code: 401
      }
    }
  }

  // 获取后台需要的文章类别
  async getArticleType() {
    // 读取数据库中 type 表信息
    const articleType = await this.app.mysql.select('type')
    this.ctx.body = {
      data: articleType,
      code: 200
    }
  }

  // 向数据库中添加文章
  async addArticle() {
    // 获取后台发送的文章
    const article = this.ctx.request.body
    // 向数据库中article表中插入数据
    const res = await this.app.mysql.insert('article', article)
    // console.log('+++++++', res, '+++++++++++')
    // 插入数据是否只影响了一行，只影响一行说明插入成功
    const insertSuccess = res.affectedRows === 1
    // 当前插入到哪行，一般是最后一行后的空行
    const insertId = res.insertId
    // 将插入成功的信息传给后台
    this.ctx.body = {
      isScuccess: insertSuccess,
      insertId: insertId,
      code: 200
    }
  }

  // 修改已有文章
  async updataArticle() {
    const newArticle = this.ctx.request.body
    // 修改数据库中数据
    const res = await this.app.mysql.update('article', newArticle)
    // console.log(res, 'rrrrrrrrr')
    // console.log(newArticle, 'nnnnnn')
    const updataSuccess = res.affectedRows === 1
    // const updataId = newArticle.id
    this.ctx.body = {
      updataSuccess: updataSuccess,
      code: 200
    }
  }

  // 获取后台文章列表页的文章列表
  async getArticleList() {
    const sql = 'SELECT article.id as id,' +
      'article.title as title,' +
      'article.introduce as introduce,' +
      'article.addTime as addTime,' +
      'article.view_count as view_count,' +
      'type.typeName as typeName ' +
      'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
      'ORDER BY article.id DESC '
    const resList = await this.app.mysql.query(sql)
    this.ctx.body = {
      articleList: resList,
      code: 200
    }
  }

  // 删除对应id文章
  async deleteArticle() {
    const id = this.ctx.params.id
    const res = await this.app.mysql.delete('article', { 'id': id })
    this.ctx.body = {
      delteData: res,
      code: 200
    }
  }

  // 根据id获取详情页文章
  async getArticleById() {
    let id = this.ctx.params.id
    // console.log(id, '+++++++++++')
    let sql = 'SELECT article.id as id ,' +
      'article.title as title ,' +
      'article.introduce as introduce ,' +
      'article.article_content as article_content ,' +
      'article.addTime as addTime ,' +
      'article.view_count as view_count ,' +
      'type.typeName as typeName, ' +
      'type.id as typeId ' +
      'FROM article LEFT JOIN type ON article.type_id = type.Id ' +
      'WHERE article.id=' + id
    const result = await this.app.mysql.query(sql)
    this.ctx.body = {
      data: result,
      code: 200
    }
  }
}

module.exports = MainController
